package com.jhhc.FloodForecast.Calculate.Forecast.Controller;

import com.jhhc.FloodForecast.Calculate.Forecast.Pojo.BDMS_CALCULATESCHEME_M;
import com.jhhc.FloodForecast.Calculate.Forecast.Pojo.CalSchemeInfo;
import com.jhhc.FloodForecast.Calculate.Forecast.Service.BDMS_CALCULATESCHEME_MService;
import com.jhhc.utils.ImageBase64;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(value="洪水预报服务",tags={"洪水预报服务"})
@RestController
public class ForecastController {

    @Autowired
    private BDMS_CALCULATESCHEME_MService BDMS_CALCULATESCHEME_MService;

    @ApiOperation(value = "最新计算方案信息", notes = "最新计算方案信息",httpMethod = "GET")
    @RequestMapping(value = "FloodForecast/Calculate/Forecast/GetNewCalSchemeInfo",method = RequestMethod.GET)
    public JsonResult GetNewCalSchemeInfo(@ApiParam(name = "ProjectId", value = "系统编号", required = true) @RequestParam Integer ProjectId
            ,@ApiParam(name = "UserID",value = "预报员编号",required = true)@RequestParam String UserID
            ,@ApiParam(name = "ForecastSchemeID",value = "预报方案编号",required = true)@RequestParam String ForecastSchemeID){
        JsonResult jsonResult = new JsonResult();
        ImageBase64 imageBase64 = new ImageBase64();
        List<BDMS_CALCULATESCHEME_M> BDMS_CALCULATESCHEME_M =
                BDMS_CALCULATESCHEME_MService.select(ProjectId, UserID, ForecastSchemeID);
        List<CalSchemeInfo> calSchemeInfos = new ArrayList<CalSchemeInfo>();
        CalSchemeInfo calSchemeInfo = null;
        for (int i = 0; i < BDMS_CALCULATESCHEME_M.size() ; i++) {
            calSchemeInfo = new CalSchemeInfo();
            if (BDMS_CALCULATESCHEME_M.get(i).getCschemeid() != null){
                calSchemeInfo.setCalSchemeID(BDMS_CALCULATESCHEME_M.get(i).getCschemeid());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getCschemenm() != null){
                calSchemeInfo.setCalSchemeName(BDMS_CALCULATESCHEME_M.get(i).getCschemenm());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getAliasnm() != null){
                calSchemeInfo.setAliasName(BDMS_CALCULATESCHEME_M.get(i).getAliasnm());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getUserid() != null){
                calSchemeInfo.setUserID(BDMS_CALCULATESCHEME_M.get(i).getUserid());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getFschemeid() != null){
                calSchemeInfo.setForecastSchemeID(BDMS_CALCULATESCHEME_M.get(i).getFschemeid());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getDtmstart() != null){
                calSchemeInfo.setForecastBeginTM(imageBase64.DateToStr(BDMS_CALCULATESCHEME_M.get(i).getDtmstart()));
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getDtmforecast() != null){
                calSchemeInfo.setForecastTM(imageBase64.DateToStr(BDMS_CALCULATESCHEME_M.get(i).getDtmforecast()));
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getDtmend() != null){
                calSchemeInfo.setForecastEndTM(imageBase64.DateToStr(BDMS_CALCULATESCHEME_M.get(i).getDtmend()));
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getDtmcalculate() != null){
                calSchemeInfo.setCalculateTM(imageBase64.DateToStr(BDMS_CALCULATESCHEME_M.get(i).getDtmcalculate()));
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getCalinfo() != null){
                String[] wfInfo = BDMS_CALCULATESCHEME_M.get(i).getCalinfo().split("\\|");
                calSchemeInfo.setWfProduct(wfInfo[0]);
                calSchemeInfo.setWfInteractive(wfInfo.length > 1 ? wfInfo[1] : null);
//                if (wfInfo.length>1){
//                    calSchemeInfo.setWfInteractive(wfInfo[1]);
//                }else {
//                    calSchemeInfo.setWfInteractive(null);
//                }
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getFlag() != null && "Y".equals(BDMS_CALCULATESCHEME_M.get(i).getFlag())){
                calSchemeInfo.setIsSaved(true);
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getPublish() != null && "1".equals(BDMS_CALCULATESCHEME_M.get(i).getPublish())){
                calSchemeInfo.setIsPublished(true);
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getRemark() != null) {
                calSchemeInfo.setRemarks(BDMS_CALCULATESCHEME_M.get(i).getRemark());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getTag() != null) {
                calSchemeInfo.setTag(BDMS_CALCULATESCHEME_M.get(i).getTag());
            }
            calSchemeInfos.add(calSchemeInfo);
        }
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(calSchemeInfos);
        return jsonResult;
    }


    @ApiOperation(value = "计算方案信息列表",notes = "计算方案信息列表",httpMethod = "GET")
    @RequestMapping(value = "FloodForecast/Calculate/Forecast/GetCalSchemeInfos",method = RequestMethod.GET)
    public JsonResult GetCalSchemeInfos(@ApiParam(name = "ProjectId",value = "编号",required = true) @RequestParam Integer ProjectId
            ,@ApiParam(name = "UserID",value = "预报员编号",required = true)@RequestParam String UserID
            ,@ApiParam(name = "ForecastSchemeID",value = "预报方案编号",required = true)@RequestParam String ForecastSchemeID
            ,@ApiParam(name = "StartTM",value = "开始时间",required = true)@RequestParam String StartTM
            ,@ApiParam(name = "EndTM",value = "结束时间",required = true)@RequestParam String EndTM
            ,@ApiParam(name = "IsCalculateTM",value = "时间查询是否为计算时的实时时间",required = true,defaultValue = "true")@RequestParam boolean IsCalculateTM
            ,@ApiParam(name = "IsSave",value = "是否保存的方案",required = true ,defaultValue = "true")@RequestParam boolean IsSave
            ,@ApiParam(name = "IsPublish",value = "方案是否发布")@RequestParam(required = false) boolean IsPublish){
        JsonResult jsonResult = new JsonResult();
        String isSave = null;
        String isPublish = null;
        if (IsSave == true){
            isSave = "Y";
        }else {
            isSave = "N";
        }
        if (IsPublish == true){
            isPublish = "1";
        }
       List<BDMS_CALCULATESCHEME_M> BDMS_CALCULATESCHEME_M =
               BDMS_CALCULATESCHEME_MService.getCalSchemeInfos(ProjectId,UserID,ForecastSchemeID
                       ,StartTM,EndTM,IsCalculateTM,isSave,isPublish);
        ImageBase64 imageBase64 = new ImageBase64();
        List<CalSchemeInfo> calSchemeInfos = new ArrayList<>();
        CalSchemeInfo calSchemeInfo = null;
        for (int i = 0; i < BDMS_CALCULATESCHEME_M.size(); i++) {
            calSchemeInfo = new CalSchemeInfo();
            if (BDMS_CALCULATESCHEME_M.get(i).getCschemeid() != null){
                calSchemeInfo.setCalSchemeID(BDMS_CALCULATESCHEME_M.get(i).getCschemeid());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getCschemenm() != null){
                calSchemeInfo.setCalSchemeName(BDMS_CALCULATESCHEME_M.get(i).getCschemenm());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getAliasnm() != null){
                calSchemeInfo.setAliasName(BDMS_CALCULATESCHEME_M.get(i).getAliasnm());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getUserid() != null){
                calSchemeInfo.setUserID(BDMS_CALCULATESCHEME_M.get(i).getUserid());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getFschemeid() != null){
                calSchemeInfo.setForecastSchemeID(BDMS_CALCULATESCHEME_M.get(i).getFschemeid());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getDtmstart() != null){
                calSchemeInfo.setForecastBeginTM(imageBase64.DateToStr(BDMS_CALCULATESCHEME_M.get(i).getDtmstart()));
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getDtmforecast() != null){
                calSchemeInfo.setForecastTM(imageBase64.DateToStr(BDMS_CALCULATESCHEME_M.get(i).getDtmforecast()));
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getDtmend() != null){
                calSchemeInfo.setForecastEndTM(imageBase64.DateToStr(BDMS_CALCULATESCHEME_M.get(i).getDtmend()));
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getDtmcalculate() != null){
                calSchemeInfo.setCalculateTM(imageBase64.DateToStr(BDMS_CALCULATESCHEME_M.get(i).getDtmcalculate()));
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getCalinfo() != null){
                String[] wfInfo = BDMS_CALCULATESCHEME_M.get(i).getCalinfo().split("\\|");
                calSchemeInfo.setWfProduct(wfInfo[0]);
                calSchemeInfo.setWfInteractive(wfInfo.length > 1 ? wfInfo[1] : null);
//                if (wfInfo.length>1){
//                    calSchemeInfo.setWfInteractive(wfInfo[1]);
//                }else {
//                    calSchemeInfo.setWfInteractive(null);
//                }
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getFlag() != null && "Y".equals(BDMS_CALCULATESCHEME_M.get(i).getFlag())){
                calSchemeInfo.setIsSaved(true);
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getPublish() != null && "1".equals(BDMS_CALCULATESCHEME_M.get(i).getPublish())){
                calSchemeInfo.setIsPublished(true);
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getRemark() != null) {
                calSchemeInfo.setRemarks(BDMS_CALCULATESCHEME_M.get(i).getRemark());
            }
            if (BDMS_CALCULATESCHEME_M.get(i).getTag() != null) {
                calSchemeInfo.setTag(BDMS_CALCULATESCHEME_M.get(i).getTag());
            }
            calSchemeInfos.add(calSchemeInfo);
        }
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(calSchemeInfos);
        return jsonResult;
    }


}
